﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Helper.Business.Implementation;
using Helper.Core.Entity;
using Helper.Business.Repository;

using NHibernate;

using System.Linq.Expressions;
using System.Linq;

namespace Helper.Business.Implementation
{
    public class UserRepository : RepositoryBase<User>, IUserRepository
    {
        public User FindByUsernameAndPassword(string Username, string Password)
        {
            try
            {
                using (ISession session = SessionFactory.OpenSession())
                {
                    User user = session.QueryOver<User>()
                        .Where(us => us.Name == Username && us.Password == Password)
                        .SingleOrDefault<User>();

                    return user;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public IEnumerable<User> FindUsersByBusiness(int BusinessId)
        {
            try
            {
                 using (ISession session = SessionFactory.OpenSession())
                {
                     IEnumerable<User> user = session.QueryOver<User>()
                        .Where(us => us.ParentBusiness.Id == BusinessId).List();
                        
                    return user;                    
                }
            }
            catch (Exception ex)
            {                
                throw new Exception(ex.Message);
            }
        }

    }
}
